﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.Electric;
using HYElectricSystemDiagram.DataStruct;

namespace HYElectricSystemDiagram.FormSet
{
	public partial class FormAddType : HYBaseForm
	{
		public FormAddType(string m_sType, string strTitle, List<string> mlstBoxSize)
		{
			this.InitializeComponent();
			this.btnAdd.Image = Resources.GetImage("Add20");
			this.btnDel.Image = Resources.GetImage("Delete20");
			this.Text = strTitle;
			this.sType = m_sType;
			this.lstString = mlstBoxSize;
			DataTable dataTable = new DataTable();
			this.TitleAll = strTitle;
			if (!(strTitle == "箱体尺寸"))
			{
				if (!(strTitle == "导线类型"))
				{
					if (!(strTitle == "线管类型"))
					{
						this.lbType.Text = "新类型:";
					}
					else
					{
						this.nType = 2;
						this.lbType.Text = "新线管:";
						dataTable.Columns.Add("常用线管类型", typeof(string));
						this.TitleAll = "常用线管类型";
					}
				}
				else
				{
					this.nType = 1;
					this.lbType.Text = "新导线:";
					dataTable.Columns.Add("常用导线类型", typeof(string));
					this.TitleAll = "常用导线类型";
				}
			}
			else
			{
				this.nType = 0;
				this.lbType.Text = "新尺寸:";
				dataTable.Columns.Add("常用箱体尺寸", typeof(string));
				this.TitleAll = "常用箱体尺寸";
			}
			foreach (string text in this.lstString)
			{
				dataTable.Rows.Add(new object[]
				{
					text
				});
			}
			this.dataGridView1.DataSource = dataTable;
			this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
			this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.tbTypeNew.Text = this.lstString[0];
		}

		private List<string> GetNodeName(string m_sType, int Type)
		{
			List<string> list = new List<string>();
			switch (Type)
			{
			case 0:
				if (m_sType.Contains("风机"))
				{
					list.Add("UserControlData1");
					list.Add("FanBoxSize");
				}
				else if (m_sType.Contains("潜污泵"))
				{
					list.Add("UserControlData1");
					list.Add("PumpBoxSize");
				}
				else if (m_sType.Contains("住户"))
				{
					list.Add("UserControlData2");
					list.Add("BoxSize");
				}
				break;
			case 1:
				if (m_sType.Contains("消防"))
				{
					list.Add("UserControlData1");
					list.Add("FireFightingLineType");
				}
				else if (m_sType.Contains("普通"))
				{
					list.Add("UserControlData1");
					list.Add("NormalLineType");
				}
				else if (m_sType.Contains("住户"))
				{
					list.Add("UserControlData2");
					list.Add("LineType");
				}
				break;
			case 2:
				if (m_sType.Contains("住户"))
				{
					list.Add("UserControlData2");
					list.Add("SpoolType");
				}
				else
				{
					list.Add("UserControlData1");
					list.Add("SpoolType");
				}
				break;
			}
			return list;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			string text = this.tbTypeNew.Text;
			if (this.TitleAll.Equals("常用箱体尺寸") && this.SubstringCount(text) != 3)
			{
				MessageBox.Show("新增类型格式不正确!");
				return;
			}
			if (string.IsNullOrEmpty(text))
			{
				MessageBox.Show("新增类型不能为空!");
				return;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < this.dataGridView1.RowCount; i++)
			{
				string text2 = this.dataGridView1[0, i].Value.ToString();
				if (text2.Equals(text))
				{
					MessageBox.Show("已经存在该类型名称!");
					return;
				}
				list.Add(text2);
			}
			DataTable dataTable = (DataTable)this.dataGridView1.DataSource;
			dataTable.Rows.Add(new object[]
			{
				text
			});
			this.dataGridView1.DataSource = dataTable;
		}

		private int SubstringCount(string str)
		{
			if (str.Contains("*"))
			{
				string[] array = str.Split(new char[]
				{
					'*'
				});
				if (array.Length == 3)
				{
					try
					{
						int.Parse(array[0]);
						int.Parse(array[1]);
						int.Parse(array[2]);
						return 3;
					}
					catch
					{
						return 0;
					}
					return 0;
				}
			}
			return 0;
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.RowCount == 1)
			{
				YJKMessageBox.Show("最后一条数据不能删除！");
				return;
			}
			string text = this.tbTypeNew.Text;
			if (MessageBox.Show("确认删除类型:" + text + "?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
			{
				return;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < this.dataGridView1.RowCount; i++)
			{
				string item = this.dataGridView1[0, i].Value.ToString();
				list.Add(item);
			}
			DataTable dataTable = (DataTable)this.dataGridView1.DataSource;
			dataTable.Rows.Clear();
			this.dataGridView1.DataSource = dataTable;
			foreach (string text2 in list)
			{
				if (!(text2 == text))
				{
					dataTable.Rows.Add(new object[]
					{
						text2
					});
				}
			}
			this.dataGridView1.DataSource = dataTable;
			int index = this.dataGridView1.CurrentRow.Index;
			this.tbTypeNew.Text = this.dataGridView1.Rows[index].Cells[0].GetText();
		}

		private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
		{
			string text = this.dataGridView1.CurrentCell.Value.ToString();
			this.tbTypeNew.Text = text;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Update();
			List<string> list = new List<string>();
			for (int i = 0; i < this.dataGridView1.RowCount; i++)
			{
				string item = this.dataGridView1[0, i].Value.ToString();
				list.Add(item);
			}
			List<string> nodeName = this.GetNodeName(this.sType, this.nType);
			if (nodeName.Count == 2)
			{
				GetXmlData.WriteNodeData(nodeName[0], nodeName[1], list);
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private int nType;

		private string sType = "";

		private List<string> lstString = new List<string>();

		private string TitleAll = "";
	}
}
